<template>
	<view class="">
		<view class="pagesBox">
			<view class="pagesMain">
				<view class="popupInput">
					<view class="inputTitle">姓名</view>
					<u-input v-model="params.consignee_name" placeholder="请输入姓名" :clearable="false" :placeholder-style="placeholderStyle" :custom-style="customStyle" type="text" :maxlength='7' :border='true' border-color="#a1a5aa" :height="96"></u-input>
				</view>
				<view class="popupInput">
					<view class="inputTitle">手机号</view>
					<u-input v-model="params.consignee_phone" placeholder="请输入手机号" :clearable="false" :placeholder-style="placeholderStyle" :custom-style="customStyle" type="number" :maxlength='11' :border='true' border-color="#a1a5aa" :height="96"></u-input>
				</view>
				<view class="popupInput">
					<view class="inputTitle">地址</view>
					<view class="myInputBox">
						<view class="myInput" @click="show = true">
							<text v-if="!params.province">请选择地址</text>
							<text v-else style="color: #000;">{{params.province}}{{params.city}}{{params.area}}</text>
							<u-icon name="map" size="48"></u-icon>
						</view>
					</view>
				</view>
				<view class="popupInput">
					<view class="inputTitle">详细地址</view>
					<u-input v-model="params.address_detail" placeholder="请输入您的详细地址" :clearable="false" :placeholder-style="placeholderStyle" :custom-style="customStyle" type="textarea" :border='true' border-color="#a1a5aa" :height="96" maxlength="200" @input="inputChange"></u-input>
					<!-- #ifdef MP-WEIXIN -->
					<!-- <view class="textNum">{{wordNum}}/200</view> -->
					<!-- #endif -->
				</view>
				<view class="popupInput">
					<view class="inputTitle">
						默认
						<u-switch v-model="params.isDefaultType" size="40" active-color="#2BA777" inactive-color="#ccc" @change="changeSwitch"></u-switch>
					</view>
				</view>
			</view>
			<view class="pagesMain" style="margin-top: 16rpx;">
				<view class="popupInput">
					<view class="inputTitle">自动识别</view>
					<u-input v-model="parse" placeholder="请粘贴您需要识别的地址" :clearable="false" :placeholder-style="placeholderStyle" :custom-style="customStyle" type="textarea" :border='false' border-color="#a1a5aa" :height="96" maxlength="200"></u-input>
				</view>
				<view class="btnBox">
					<view class="btn" @click="analysis">点击识别</view>
				</view>
			</view>
			<my-submitBtn :isFixed="false" v-if="type == 1" btnText="确认添加" @click="submitBefore"></my-submitBtn>
			<my-submitBtn :isFixed="false" v-if="type == 2" btnText="确认修改" @click="submitBefore"></my-submitBtn>
			<view class="submitBox">
				<view class="btn btn1" hover-class="btnHover" @click="getWxAddress">导入微信地址</view>
			</view>
			<u-picker mode="region" v-model="show" @confirm="selectAddress"></u-picker>
			<u-picker mode="selector" v-model="showProvince" @confirm="confirmProvince"  :default-selector="defaultProvince" :range="selectorProvince" range-key="label"></u-picker>
			<u-picker mode="selector" v-model="showCity" @confirm="confirmCity"  :default-selector="defaultCity" :range="selectorCity" range-key="label"></u-picker>
			<u-picker mode="selector" v-model="showArea" @confirm="confirmArea"  :default-selector="defaultArea" :range="selectorArea" range-key="label"></u-picker>
		</view>
	</view>
</template>

<script>
	import AddressParse from 'address-parse';
	export default {
		data() {
			return {
				baseUrl:this.$baseUrl,
				// 输入框样式
				customStyle:{fontSize: '32rpx',color:'#000'},
				placeholderStyle:"font-size:32rpx;color:'#000'",
				params:{
					consignee_name:'',
					consignee_phone:'',
					province:'',
					city:'',
					area:'',
					address_detail:'',
					id:'',
					is_default:1,
					isDefaultType:true
				},
				// 1添加，2修改
				type:1,
				wordNum:0,
				// 选择地区弹出窗
				show:false,
				// 防抖
				time:'',
				// 识别
				parse:""
			}
		},
		onLoad(options) {
			this.type = options.type;
			// this.getProvince()
			if(options.type == 2){
				this.params.id = options.id;
				this.getAddressDetail()
			}else{
				delete this.params.id
			}
		},
		methods: {
			// 修改信息时，获取原本信息
			async getAddressDetail() {
				let res = await uni.$http("POST","address/detail",{id:this.params.id});
				if(res.code == 1){
					this.params.consignee_name = res.data.consignee_name
					this.params.consignee_phone = res.data.consignee_phone
					this.params.province = res.data.province
					this.params.city = res.data.city
					this.params.area = res.data.area
					this.params.address_detail = res.data.address_detail;
					this.params.is_default = res.data.is_default;
				}
			},
			// 添加信息
			submitBefore() {
				if (!this.params.consignee_name) {
					uni.$u.toast('请先填写姓名')
				} else if (!this.$u.test.mobile(this.params.consignee_phone)) {
					uni.$u.toast('请填写正确手机号')
				} else if (!this.params.province) {
					uni.$u.toast('请选择收货地址')
				} else if (!this.params.address_detail) {
					uni.$u.toast('请填写详细地址')
				}else{
					if(this.time){
						clearTimeout(this.time)
						this.time="";
					}
					uni.showLoading({
						title:'加载中',
						mask:true
					})
					this.time = setTimeout(()=>{
						this.submit()
					},1000)
				}
			},
			async submit() {
				let res = await uni.$http("POST","address/edit",this.params);
				console.log('添加收货地址',res);
				uni.hideLoading()
				uni.$u.toast(res.msg)
				if (res.code == 1){
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				}else{
					this.params.isDefaultType = true;
				}
			},
			// 选择地址
			selectAddress(e){
				console.log(e);
				this.params.province = e.province.label;
				this.params.city = e.city.label;
				this.params.area = e.area.label;
			},
			changeSwitch(e){
				console.log(e);
				if(!e){
					this.params.is_default = 0;
					this.params.isDefaultType = false;
				}else{
					this.params.is_default = 1;
					this.params.isDefaultType = true;
				}
			},
			// 字数变化
			inputChange(){
				if(this.params.address_detail){
					if(this.params.address_detail.length <= 200){
						this.wordNum = this.params.address_detail.length;
					}else{
						this.wordNum = 200;
					}
				}else{
					this.wordNum = 0;
				}
			},
			// 解析地址
			analysis() {
				let data = this.parse.replaceAll("姓名","").replaceAll("手机号","").replaceAll("收货地址","").replaceAll("地址","")
				.replaceAll("收件人","").replaceAll("手机号码","").replaceAll("所在地区","").replaceAll("详细地址","")
				const res = AddressParse.parse(data)[0]
				console.log("res",res);
				this.params.consignee_name = res.name;
				this.params.consignee_phone = res.mobile;
				this.params.province = res.province;
				this.params.city = res.city;
				this.params.area = res.area;
				this.params.address_detail = res.details;
			},
			// 导入微信地址
			getWxAddress(){
				uni.chooseAddress({
				  success:(res)=> {
					this.params.consignee_name = res.userName;
					this.params.consignee_phone = res.telNumber;
					this.params.province = res.provinceName;
					this.params.city = res.cityName;
					this.params.area = res.countyName;
					this.params.address_detail = res.detailInfo;
				  },
				  fail: (err) => {
				  	 console.log(err)
				  }
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.pagesBox{
		padding: 12rpx 24rpx;
		position: relative;
		.pagesMain{
			background-color: #fff;
			border-radius: 16rpx;
			padding: 24rpx;
		}
		.popupInput{
			margin-bottom: 32rpx;
			position: relative;
			.inputTitle{
				font-weight: 600;
				font-size: 28rpx;
				color: #000;
				margin-bottom: 10rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
			}
			// 自定义输入框
			.myInputBox{
				display: flex;
				justify-content: space-between;
				align-items: center;
				.myInput{
					width: 100%;
					height: 96rpx;
					line-height: 96rpx;
					font-weight: 400;
					font-size: 32rpx;
					color: #808080;
					// background-color: #F7F8FA;
					border: 2rpx solid #a1a5aa;
					border-radius: 8rpx;
					padding: 0 24rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
				}
			}
			// 字数
			.textNum{
				color: #A1A5AA;
				position: absolute;
				bottom: 12rpx;
				right: 16rpx;
			}
			.addressBox{
				display: flex;
				justify-content: space-between;
				align-items: center;
				.addressItem{
					width: 32.5%;
					height: 96rpx;
					font-weight: 400;
					font-size: 32rpx;
					color: #000;
					// background-color: #F7F8FA;
					border: 2rpx solid #a1a5aa;
					border-radius: 6rpx;
					padding: 0 24rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;
				}
			}
		}
		.btnBox{
			display: flex;
			justify-content: flex-end;
			.btn{
				width: 176rpx;
				height: 64rpx;
				font-weight: 400;
				font-size: 26rpx;
				color: #73797F;
				background: #F0F0F0;
				border-radius: 48rpx 48rpx 48rpx 48rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		.submitBox{
			height: 130rpx;
			padding: 0 16rpx;
			letter-spacing: 2rpx;
			display: flex;
			justify-content: space-around;
			.btn{
				width: 80%;
				height: 80rpx;
				color: #2BA777;
				font-size: 30rpx;
				font-weight: 600;
				border: 2rpx solid #2BA777;
				border-radius: 40rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.btn1{
				background: #FFFFFF;
			}
			.btnHover{
				background: #f7fff8;
			}
		}
	}
</style>
<style>
	page{
		background-color: #f7f8fa;
	}
</style>